﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pavilion.Communications;

namespace Pavilion.DesignerTool
{
    static public class LinkTransfer
    {
        static public void Add(LinkWrapper linkWrapper)
        {
            ContainerNodeWrapper containerNodeWrapperParent = linkWrapper.ContainerWrapperParent;
            string scriptName = containerNodeWrapperParent.NodeScriptWrapper.Name;

            Add(containerNodeWrapperParent.NodeScriptWrapper.ResourceData.ResourceName, scriptName, containerNodeWrapperParent.ContainerNode, linkWrapper.Link);
        }

        static public void Add(string resourceName, string scriptName, ContainerNode parentContainer, Link link)
        {
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(parentContainer);

            DataTransferClient.Instance.TransferData((proxy) => proxy.AddLink(resourceName, scriptName, containerNodeNameList, link.StartingNode.Name, link.EndingNode.Name, link.OpenCriteria.ToString()));

            foreach (ILock lockItem in link.LockCollection)
                LockTransfer.Add(resourceName, scriptName, parentContainer, link, lockItem);
        }

        internal static void Remove(LinkWrapper linkWrapper)
        {
            ContainerNode parentContainer = linkWrapper.ContainerWrapperParent.ContainerNode;
            var resourceName = linkWrapper.ContainerWrapperParent.NodeScriptWrapper.ResourceData.ResourceName;
            string scriptName = linkWrapper.ContainerWrapperParent.NodeScriptWrapper.Name;
            var linkIndex = parentContainer.LinkCollection.IndexOf(linkWrapper.Link);

            Remove(resourceName, scriptName, parentContainer, linkIndex);
        }

        static public void Remove(string resourceName, string scriptName, ContainerNode parentContainer, int linkIndex)
        {
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(parentContainer);

            DataTransferClient.Instance.TransferData((proxy) => proxy.RemoveLink(resourceName, scriptName, containerNodeNameList, linkIndex));
        }

        static public void Update(LinkWrapper linkWrapper)
        {
            ContainerNodeWrapper containerNodeWrapperParent = linkWrapper.ContainerWrapperParent;
            string scriptName = containerNodeWrapperParent.NodeScriptWrapper.Name;

            UpdateOpenCriteria(containerNodeWrapperParent.NodeScriptWrapper.ResourceData.ResourceName, scriptName, containerNodeWrapperParent.ContainerNode, linkWrapper.Link);
        }

        static public void UpdateOpenCriteria(string resourceName, string scriptName, ContainerNode parentContainer, Link link)
        {
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(parentContainer);
            var linkIndex = parentContainer.LinkCollection.IndexOf(link);

            DataTransferClient.Instance.TransferData((proxy) => proxy.UpdateLinkOpenCriteria(resourceName, scriptName, containerNodeNameList, linkIndex, link.OpenCriteria.ToString()));
        }
    }
}
